package com.lenovo.smarttraffic.ui.activity;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.lenovo.smarttraffic.R;

import java.util.ArrayList;
import java.util.List;

public class Activity_1_2 extends AppCompatActivity {
    private ImageView fanhuiBtn;
    private Spinner spinner1;
    private Button chaxunBtn;
    private ListView listview1;
    private MyAdapter myAdapter;
    private SQLiteDatabase db;
    private List<String[]> shuju_list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_1_2);
        init();

    }
    private void init(){
        db = new Activity_1_Mysqlite3(Activity_1_2.this,"user1.db",null,1).getWritableDatabase();

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        fanhuiBtn = (ImageView) findViewById(R.id.fanhui_btn);
        fanhuiBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        spinner1 = (Spinner) findViewById(R.id.spinner1);
        chaxunBtn = (Button) findViewById(R.id.chaxun_btn);
        chaxunBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (spinner1.getSelectedItemPosition()){
                    case 0:chaxun_shuju(1);break;
                    case 1:chaxun_shuju(0);break;
                }
            }
        });
        listview1 = (ListView) findViewById(R.id.listview1);
        View head_view = getLayoutInflater().inflate(R.layout.activity_1_head_view,null);
        listview1.addHeaderView(head_view);
        chaxun_shuju(1);

    }
    private void chaxun_shuju(int method){
        shuju_list = new ArrayList<>();
        Cursor cursor;
        if(method==0){
             cursor = db.query("user1",null,null,null,null,null,null,null);

        }else {
             cursor = db.query("user1",null,null,null,null,null,"time desc",null);
        }
        while (cursor.moveToNext()){
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            int car_number = cursor.getInt(cursor.getColumnIndex("car_number"));
            int money = cursor.getInt(cursor.getColumnIndex("money"));
            String user = cursor.getString(cursor.getColumnIndex("user"));
            String time = cursor.getString(cursor.getColumnIndex("time"));

            shuju_list.add(new String[]{
                    String.valueOf(id),
                    String.valueOf(car_number),
                    String.valueOf(money),
                    user,
                    time
            });
        }
        myAdapter = new MyAdapter();
        listview1.setAdapter(myAdapter);
    }
    class MyAdapter extends BaseAdapter{
        private TextView xuhao;
        private TextView chehao;
        private TextView moneyLisi;
        private TextView caozuoren;
        private TextView chongzhishijian;
        @Override
        public int getCount() {
            return shuju_list.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = getLayoutInflater().inflate(R.layout.activity_1_head_view,null);

            xuhao = (TextView) view.findViewById(R.id.xuhao);
            chehao = (TextView) view.findViewById(R.id.chehao);
            moneyLisi = (TextView) view.findViewById(R.id.money_lisi);
            caozuoren = (TextView) view.findViewById(R.id.caozuoren);
            chongzhishijian = (TextView) view.findViewById(R.id.chongzhishijian);
                xuhao.setText(shuju_list.get(position)[0]);
                chehao.setText(Integer.valueOf(shuju_list.get(position)[1])+1+"");
                moneyLisi.setText(shuju_list.get(position)[2]);
                caozuoren.setText(shuju_list.get(position)[3]);
                chongzhishijian.setText(shuju_list.get(position)[4]);
            return view;
        }
    }
}
